package com.cat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cat.dto.PageDTO;
import com.cat.dto.UserInfoDTO;
import com.cat.entity.AddressBook;
import com.cat.entity.UserInfo;
import com.cat.mapper.AddressBookMapper;
import com.cat.mapper.UserInfoMapper;
import com.cat.query.UserQuery;
import com.cat.service.IUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cat.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lmy
 * @since 2024-09-11
 */
@Service
@RequiredArgsConstructor
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    private final UserInfoMapper userInfoMapper;

    private final AddressBookMapper addressBookMapper;

    @Override
    public PageDTO<UserInfoVO> queryUserPage(UserQuery userQuery) {
        // 1.构建分页查询
        Page<UserInfo> page = Page.of(userQuery.getPageNum(), userQuery.getPageSize());

        // 2.分页查询
        Page<UserInfo> p = lambdaQuery()
                .like(userQuery.getUsername() != null, UserInfo::getUsername, userQuery.getUsername())
                .eq(userQuery.getStatus() != null, UserInfo::getStatus, userQuery.getStatus())
                .eq(userQuery.getGender() != null, UserInfo::getGender, userQuery.getGender())
                .gt(userQuery.getMinAge() != null, UserInfo::getAge, userQuery.getMinAge())
                .lt(userQuery.getMaxAge() != null, UserInfo::getAge, userQuery.getMaxAge())
                .gt(userQuery.getStartTime() != null, UserInfo::getCreateTime, userQuery.getStartTime())
                .lt(userQuery.getEndTime() != null, UserInfo::getCreateTime, userQuery.getEndTime())
                .page(page);

        // 3.封装VO
        PageDTO<UserInfoVO> dto = new PageDTO<UserInfoVO>();
        dto.setPageSize(p.getSize());
        dto.setTotal(p.getTotal());
        dto.setPageNum(p.getCurrent());

        List<UserInfo> records = p.getRecords();
        if (CollUtil.isEmpty(records)) {
            // Collections.emptyList() 返回一个空列表
            dto.setList(Collections.emptyList());
        } else {
            List<UserInfoVO> vos = BeanUtil.copyToList(records, UserInfoVO.class);
            dto.setList(vos);
        }

        return dto;
    }

    @Override
    @Transactional
    public void deleteAndAddress(List<Integer> ids) {
        userInfoMapper.deleteByIds(ids);
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<AddressBook>()
                .in(AddressBook::getUserId, ids);
        addressBookMapper.delete(queryWrapper);
    }

    @Override
    public void addUser(UserInfoDTO userInfoDTO) {
        UserInfo userInfo = BeanUtil.copyProperties(userInfoDTO, UserInfo.class);


        userInfoMapper.insert(userInfo);
    }
}
